package com.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import com.alibaba.fastjson.JSONObject;

import COM.CCB.EnDecryptAlgorithm.MCipherEncryptor;
import ccb.pay.api.util.CCBPayUtil;

public class CCBUtil {

	//银行接口url
	public static String host = "https://ibsbjstar.ccb.com.cn/CCBIS/B2CMainPlat_00_BEPAY?";
	//商户信息
	public static String merInfo = "MERCHANTID=105000075232704&POSID=042065759&BRANCHID=320000000";
	//获取柜台完整公钥
	public static String pubKey = "70115821c998b0fda9ede011020111";

	public static String title = "MERCHANTID=105505200000176&POSID=042434392&BRANCHID=410000000";


	public Boolean payTest(String orderId,String authNo,String amount) {
		try {
			// 1.创建扣款请求参数串
			// 1.创建扣款请求参数串

			String strSrcParas = title + "&MERFLAG=1&TERMNO1=&TERMNO2=&ORDERID=" + orderId + "&AUTHNO=" + authNo + "&AMOUNT=" + amount + "&TXCODE=WGZF00";// 待加密参数串
			//String strSrcParas = title + "&MERFLAG=1&TERMNO1=&TERMNO2=&ORDERID=" + orderId + "&AUTHNO=" + authNo + "&AMOUNT=" + amount + "&TXCODE=WGZF00";// 待加密参数串
			// 2.创建COM.CCB. MCipherEncryptor对象
			String strKey = "9ff3a32155cc9bb9de30538b020111";// 商户公钥后30位
			//MCipherEncryptor ccbEncryptor = new MCipherEncryptor(strKey);// 创建加密对象
			MCipherEncryptor ccbEncryptor = new MCipherEncryptor(strKey);// 创建加密对象
			System.out.println(" ccbEncryptor:"+ ccbEncryptor);
			// 3.执行加密
			try {
				String ccbParam = ccbEncryptor.doEncrypt(strSrcParas);// 加密
				System.out.println(" ccbParam:"+ ccbParam);
				// 请求支付接口
				// 测试环境域名为 http://juhetest.ccb.com/
				// 生产环境域名请用 https://ibsbjstar.ccb.com.cn/
				//String url = "https://ibsbjstar.ccb.com.cn/CCBIS/B2CMainPlat_00_ENPAY?" +title+ "&ccbParam=" + ccbParam;
				//System.out.println(url);
			
				
				String url = "https://ibsbjstar.ccb.com.cn/CCBIS/B2CMainPlat_00_ENPAY?" +title+ "&ccbParam=" + ccbParam;
				System.out.println(url);

				// 发送http请求
				String ret = HttpClientUtil.httpPost(url, null, "UTF-8",""); 	//请求二维码生成链接串
		        System.out.println("ret::"+ret);
		        
				JSONObject jsonObject = JSONObject.parseObject(ret);
				System.out.println("jsonObject:"+jsonObject);
				String r = jsonObject.getString("RESULT");
				System.out.println(" r :"+ r);
				if(r.equals("Y")) {
					return true;
				}else {
					return false;
				}
		        
				

				// 发送http请求
				//String ret = HttpClientUtil.httpPost(url, null, "UTF-8",""); 	//请求二维码生成链接串
//				String ret = OkHttpUtils.getInstance().postJson(url);
//				HttpUtil.sendPost(url);
//				System.out.println("ret::"+ret);
//				JSONObject jsonObject = JSONObject.parseObject(ret);
//				System.out.println("jsonObject:"+jsonObject);
//				String r = jsonObject.getString("RESULT");
//				System.out.println(" r :"+ r);
//				if(r.equals("Y")) {
//					return true;
//				}else {
//					return false;
//				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				System.out.println(" r :"+ e.getMessage());
				return false;
			}
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println(" r 1 :"+ e.getMessage());
			return false;
		}
	}

	//	public static Boolean CCBPayUtil(String orderId,String plateNumber,String money) throws Exception{
	//		
	//		//加密原串
	//		String param =title + "&MERFLAG=1&TERMNO1=&TERMNO2=&ORDERID=" + orderId + "&AUTHNO=" + authNo + "&AMOUNT=" + amount + "&TXCODE=WGZF00";
	//		
	//		//执行加密操作
	//		CCBPayUtil ccbPayUtil = new CCBPayUtil();
	//		String url = ccbPayUtil.makeCCBParam(param, pubKey);
	//		//拼接请求串
	//		url = host + merInfo + "&ccbParam=" + url;
	//		System.out.println(url);
	//		
	//		//请求的URL如下所示：
	//		/*
	//		https://ibsbjstar.ccb.com.cn/CCBIS/B2CMainPlat_00_BEPAY?MERCHANTID=105910100190000&POSID=000000000&BRANCHID=610000000
	//		&ccbParam=加密结果...
	//		 */
	//		
	//		//向建行网关发送请求交易...
	//		String result = HttpUtil.sendPost(url);
	//		System.out.println(result);
	//		JSONObject jsonObject = JSONObject.parseObject(result);
	//		String r = jsonObject.getString("AUTHSTATUS");
	//		if(r.equals("1")) {
	//			return true;
	//		}else {
	//			return false;
	//		}
	//	}


	/**
	 * 商户通知验签DEMO，该示例以互联网银企直连被扫的PAY100接口为例
	 */
	public static void testCCBNotifyCheck() {
		//商户通知参数
		String notifyURLParam = "RESULT=Y&ORDERID=151677281312212&AMOUNT=0.01&WAITTIME=null&TRACEID=1010115031516772964428432&SIGN=80c3298a47b26cb9d8d708e1465c6b521edcce32b0deecab91257a3f41fc6" +
				"cf39fa43afa54dc8489a04615eee9dcca1f4b52ce677f70109f29745ff34033018353b78e982cc860623b6c3df0d9c1a62ca010a019fff8544d4d8e154a010d7fc16cb590ccd87f34d8bea6added68cf1f9943fdb1d836" +
				"16507a4588b68774b9fe1";
		int i = notifyURLParam.indexOf("&SIGN=");
		//获取签名内容原串
		String strSrc = notifyURLParam.substring(0, i);
		//获取数字签名域
		String sign = notifyURLParam.substring(i+6, notifyURLParam.length());
		//商户柜台完整公钥
		String pubKey = "30819d300d06092a864886f70d010101050003818b0030818702818100a32fb2d51dda418f65ca456431bd2f4173e41a82bb75c2338a6f649f8e9216204838d42e2a028c79cee19144a72b5b46fe6a498367bf41" +
				"43f959e4f73c9c4f499f68831f8663d6b946ae9fa31c74c9332bebf3cba1a98481533a37ffad944823bd46c305ec560648f1b6bcc64d54d32e213926b26cd10d342f2c61ff5ac2d78b020111";
		//验证签名数据
		CCBPayUtil ccbPayUtil = new CCBPayUtil();
		boolean b = ccbPayUtil.verifyNotifySign(strSrc,sign,pubKey);
		if(b){
			System.out.println("签名验证成功！");
		} else {
			System.out.println("签名验证失败！");
		}
	}

	public static String getRandomString2(int length){
		Random random=new Random();
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<length;i++){
			int number=random.nextInt(3);
			long result=0;
			switch(number){
			case 0:
				result=Math.round(Math.random()*25+65);
				sb.append(String.valueOf((char)result));
				break;
			case 1:
				result=Math.round(Math.random()*25+97);
				sb.append(String.valueOf((char)result));
				break;
			case 2:     
				sb.append(String.valueOf(new Random().nextInt(10)));
				break;
			}


		}
		return sb.toString();
	}
	public static Boolean payBeSwept(String orderId,String authNo,String amount,String qrcode) {
		String strSrcParas = merInfo + "&MERFLAG=1&TERMNO1=&TERMNO2=&ORDERID=" + orderId + "&AUTHNO=" + authNo + "&AMOUNT=" + amount + "&TXCODE=PAY100&QRCODE="+qrcode;// 待加密参数串
		MCipherEncryptor ccbEncryptor = new MCipherEncryptor(pubKey);// 创建加密对象
		System.out.println(" ccbEncryptor:"+ ccbEncryptor);
		// 3.执行加密
		try {
			String ccbParam = ccbEncryptor.doEncrypt(strSrcParas);// 加密
			System.out.println(" ccbParam:"+ ccbParam);
			// 请求支付接口
			// 测试环境域名为 http://juhetest.ccb.com/
			// 生产环境域名请用 https://ibsbjstar.ccb.com.cn/
			//String url = "https://ibsbjstar.ccb.com.cn/CCBIS/B2CMainPlat_00_ENPAY?" +title+ "&ccbParam=" + ccbParam;
			//System.out.println(url);
		
			
			String url = "https://ibsbjstar.ccb.com.cn/CCBIS/B2CMainPlat_00_ENPAY?" +merInfo+ "&ccbParam=" + ccbParam;
			System.out.println(url);

			// 发送http请求
			String ret = HttpClientUtil.httpPost(url, null, "UTF-8",""); 	//请求二维码生成链接串
	        System.out.println("ret::"+ret);
	        
			JSONObject jsonObject = JSONObject.parseObject(ret);
			System.out.println("jsonObject:"+jsonObject);
			String r = jsonObject.getString("RESULT");
			System.out.println(" r :"+ r);
			boolean boo = false;
			if(r.equals("Q") || r.equals("U")) {
				boo = polling(orderId,authNo,amount,qrcode,101);
				return boo;
			}
			if(r.equals("Y")) {
				return true;
			}else {
				return false;
			}
	}catch (Exception e) {
		return false;
	}
	}
		private static boolean polling(String orderId, String authNo, String amount, String qrcode,int count) {
			int type = 0;
			String stringSub = qrcode.substring(0, 1);
			switch(stringSub) {
			case "13":
				type = 2;
				break;
			case "28":
				type = 3;
				break;
			
			}
			String strSrcParas = merInfo + "&MERFLAG=1&TERMNO1=&TERMNO2=&ORDERID=" + orderId + "&TXCODE=PAY"+count+"&QRCODETYPE="+type;// 待加密参数串
			MCipherEncryptor ccbEncryptor = new MCipherEncryptor(pubKey);// 创建加密对象
			System.out.println(" ccbEncryptor:"+ ccbEncryptor);
			// 3.执行加密
			try {
				String ccbParam = ccbEncryptor.doEncrypt(strSrcParas);// 加密
				System.out.println(" ccbParam:"+ ccbParam);
				// 请求支付接口
				// 测试环境域名为 http://juhetest.ccb.com/
				// 生产环境域名请用 https://ibsbjstar.ccb.com.cn/
				//String url = "https://ibsbjstar.ccb.com.cn/CCBIS/B2CMainPlat_00_ENPAY?" +title+ "&ccbParam=" + ccbParam;
				//System.out.println(url);
			
				
				String url = "https://ibsbjstar.ccb.com.cn/CCBIS/B2CMainPlat_00_ENPAY?" +merInfo+ "&ccbParam=" + ccbParam;
				System.out.println(url);

				// 发送http请求
				String ret = HttpClientUtil.httpPost(url, null, "UTF-8",""); 	//请求二维码生成链接串
		        System.out.println("ret::"+ret);
		        
				JSONObject jsonObject = JSONObject.parseObject(ret);
				System.out.println("jsonObject:"+jsonObject);
				String r = jsonObject.getString("RESULT");
				if(r.equals("Q") || r.equals("U")) {
					polling(orderId,authNo,amount,qrcode,count+1);
				}
				if(r.equals("Y")) {
					return true;
				}else {
					return false;
				}
			}catch (Exception e) {
				return false;
			}
		
	}
}
